/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accountManagement.managedbean;

import accountManagement.entity.Employee;
import accountManagement.entity.Role;
import accountManagement.sessionbean.EmployeeManager;
import accountManagement.sessionbean.PasswordHashSessionBean;
import accountManagement.sessionbean.RoleManager;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLIntegrityConstraintViolationException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Wei Haoyang
 */
@ManagedBean
@ViewScoped
public final class AddEmployeeManagedBean implements Serializable{

    /** Creates a new instance of AddEmployeeManagedBean */
        
    @EJB
    private EmployeeManager employeeManager;
    @EJB
    private RoleManager roleManager;
    @EJB
    private PasswordHashSessionBean passwordHashSessionBean;
    
    private Employee employee;
    private String stringRoles;
    private List<Role> allRoles;
    private String[] names;
    private String[] selected;
    private List<String> selectedRoles;
    private boolean show;
    
    public AddEmployeeManagedBean() {
        employee = new Employee();
        selectedRoles = new ArrayList<String>();
    }
    
    public void saveNewEmployee(ActionEvent event) throws IOException
    {
        for(String roleId:selectedRoles)
        {
            System.out.println("********* " + roleId);
        }
        
        /*Collection<Role> roles = retrieveRoles(tokenizeSentence(stringRoles));
        
        getEmployee().setRoles(roles);
        */
        
        pushToRoles();
        System.out.print("***********************"+employee.getPassword());
   
        employee.setPassword(passwordHashSessionBean.hashPassword(employee.getPassword()));
        System.out.print("***********************"+employee.getPassword());
        try{
        employeeManager.addEmployee(employee);
        }catch(Exception ex){
            System.out.println("User Name conflict");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "User Name Already Exists", ""));
            return;
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "New Employee saved.", ""));
        /*FacesContext.getCurrentInstance().getExternalContext().redirect("debug.xhtml");
        /*try {
            Thread.currentThread().sleep(3000);
            
            
        } catch (InterruptedException e) {
        }
        
        FacesContext.getCurrentInstance().getExternalContext().redirect("AddEmployee.xhtml");
         */
        setShow(true);
    }
    
    public void oneMore(ActionEvent event) throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect("AddEmployee.xhtml");
    }
    
    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    
/*    public static String[] tokenizeSentence(String str) {
        StringTokenizer tokenizer = new StringTokenizer(str, ",");
        List tempList = new ArrayList();
        while (tokenizer.hasMoreElements()) {
            tempList.add(tokenizer.nextElement());
        }
        String[] ret = new String[tempList.size()];
        tempList.toArray(ret);
        return ret;
    }
    
    public Collection<Role> retrieveRoles(String[] str){
        Collection<Role> roles = new ArrayList<Role>();
        int i = 0;
        Role role;
        
        do{
            role = roleManager.getRoleByName(str[i]);
            roles.add(role);
        }while(str[i] != null);
        
        return roles;
    }

    /**
     * @return the stringRoles
     */
    public String getStringRoles() {
        return stringRoles;
    }

    /**
     * @param stringRoles the stringRoles to set
     */
    public void setStringRoles(String stringRoles) {
        this.stringRoles = stringRoles;
    }

    /**
     * @return the allRoles
     */
    public List<Role> getAllRoles() {
        return allRoles;
    }

    /**
     * @param allRoles the allRoles to set
     */
    public void setAllRoles(List<Role> allRoles) {
        this.setAllRoles(allRoles);
    }

    /**
     * @return the employeeManager
     */
    public EmployeeManager getEmployeeManager() {
        return employeeManager;
    }

    /**
     * @param employeeManager the employeeManager to set
     */
    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    /**
     * @return the roleManager
     */
    public RoleManager getRoleManager() {
        return roleManager;
    }

    /**
     * @param roleManager the roleManager to set
     */
    public void setRoleManager(RoleManager roleManager) {
        this.roleManager = roleManager;
    }
    
    public void retrieveNames(List<Role> roles){
        int i = 0;
        while (roles.get(i) !=null){
            getNames()[i] = roles.get(i).getRoleName();
            i++;
        }
    }

    /**
     * @return the names
     */
    public String[] getNames() {
        return names;
    }

    /**
     * @param names the names to set
     */
    public void setNames(String[] names) {
        this.names = names;
    }

    /**
     * @return the selected
     */
    public String[] getSelected() {
        return selected;
    }

    /**
     * @param selected the selected to set
     */
    public void setSelected(String[] selected) {
        this.selected = selected;
    }

    public List<String> getSelectedRoles() {
        return selectedRoles;
    }

    public void setSelectedRoles(List<String> selectedRoles) {
        this.selectedRoles = selectedRoles;
    }
    
    public void pushToRoles(){
        int i = 0;
        Long id;
        id = Long.valueOf(selectedRoles.get(i));
        employee.getRoles().add(roleManager.getRole(id));
        System.out.println(selectedRoles.get(i));
        
        while(i < (selectedRoles.size()-1)){
            i++;
            id = Long.valueOf(selectedRoles.get(i));
            employee.getRoles().add(roleManager.getRole(id));
            System.out.println(selectedRoles.get(i));
        }
        
    }

    /**
     * @return the show
     */
    public boolean isShow() {
        return show;
    }

    /**
     * @param show the show to set
     */
    public void setShow(boolean show) {
        this.show = show;
    }
}
